package com.ganee.agora.adapter

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ganee.agora.R
import com.ganee.agora.manager.RecyclerViewHolder
import com.ganee.agora.model.HomeListBean
import com.ganee.baselib.arouter.Constance

/**
 *    @author : EricCui
 *    date   : 2020-01-01 17:45
 *    desc   :
 */
class HomeAdapter (entityList: MutableList<HomeListBean>) : BaseQuickAdapter<HomeListBean,
        RecyclerViewHolder>(R.layout.item_recyclerview_main,entityList){


    override fun convert(helper: RecyclerViewHolder, item: HomeListBean?) {

        val recyclerView = helper.getView<RecyclerView>(R.id.itemRecyclerView) as RecyclerView
        val itemTitle = helper.getView<TextView>(R.id.itemTitle) as TextView

        recyclerView.setHasFixedSize(false)
        val manager = LinearLayoutManager(mContext)
        manager.orientation = LinearLayoutManager.HORIZONTAL
        recyclerView.layoutManager = manager

        if(item != null) {
            itemTitle.text = item.sectionTitle
            val mList = mutableListOf<HomeListBean.DataAryBean>()

            mList.addAll(item.dataAry)

            if ("Recommend for you" == item.sectionTitle){
                val mRecommendAdapter = HomeTopAdapter(mList)
                recyclerView.adapter = mRecommendAdapter
            } else {
                val homeItemAdapter = HomeBottomAdapter(mList)
                recyclerView.adapter = homeItemAdapter
            }
            recyclerView.addOnItemTouchListener(object : com.chad.library.adapter.base.listener.OnItemClickListener() {

                override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                    val entity = adapter.getItem(position) as HomeListBean.DataAryBean
                    ARouter.getInstance()
                            .build(Constance.ACTIVITY_URL_HOME_DETAIL)
                            .withString("activeId",entity.activeId)
                            .navigation(mContext)
                }
            })

        }

    }

}